const  state={
    vistebviews:[]
}
const actions= {
    addView({ dispatch }, view) {
        dispatch('addVisitedView', view)
    },
    addVisitedView({ commit }, view) {
        commit('ADD_VISITED_VIEW', view)
    },
    closeView({ dispatch , state}, view) {
        return new Promise(resolve =>{
            dispatch('closeViewVisitedView', view)
            resolve({
                vistebviews:[...state.vistebviews]
            })
        })
    },
    closeViewVisitedView({ commit ,state }, view) {
        return new Promise(resolve =>{
            commit('CLOSE_VISITED_VIEW', view)
            resolve([...state.vistebviews])
        })
    },
}
const mutations= {
    ADD_VISITED_VIEW(state,view){
        if (state.vistebviews.some(v => v.path === view.path)) return
        state.vistebviews.push(
            Object.assign({}, view, {
            title: view.meta.title || 'no-name'
            })
        );
    },
    CLOSE_VISITED_VIEW:(state,view) =>{
        for(const [i,v] of state.vistebviews.entries()){
            if(v.path === view.path){
                state.vistebviews.splice(i,1);
                break
            }
        }
    },
}
export default {
    namespaced: true,
    state,
    mutations,
    actions
  }